<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户管理</title>
<jsp:include page="../inc.jsp"></jsp:include>
<script type="text/javascript"><!--
	Ext.onReady(function(){
		Ext.QuickTips.init();
		createGrid();
		createLayout();
		createForm();
		form = Ext.create('userForm');
	});
	
	var gridEl = 'repTime-grid';
	var grid, form, selModel, toolbar, searchFlds;
	var win;
	
	function createGrid(){
		var queryFlds = new Array();
		searchFlds = new Ext.util.MixedCollection();
		var fldTimeName = new Ext.form.TextField({
			name : 'name',
			id : gridEl + '-name',
			fieldLabel : '名称',
			allowBlank : true,
			width : 200
		});

		searchFlds.add('name', fldTimeName);
		grid =	Ext.create('gridView',{
			remoteSort : true,
			gridId : gridEl,
			dataAutoLoad:false,
			checkboxSelectionModel:true,
			queryFlds : queryFlds,
			searchFlds : searchFlds,
			entityClassName : 'com.rover.entity.system.CodeValue',
			queryUrl : '../codeValueController/query.action',
			deleteUrl : '../codeValueController/delete.action',
			jsonId : 'id',
			recordPk : [ "id" ]
		});
		
		 grid.getStore().on('beforeload', function(store, options) {
	          store.proxy.extraParams.codeId='<%=request.getParameter("codeId")%>';
	        }, this, true);
	}
	
	// 刷新列表
	function reloadRecords() {
		grid.getStore().reload();
	}

	function createWindows() {
		if (win == null) {
			win = new Ext.Window({
				title : '新增',
				iconCls : 'icon-win',
				autoHeight : true,
				width : 335,
				height : 200,
				buttonAlign : 'center',
				buttons : [ {
					text : "保存",
					handler : function() {
						if (form.getForm().isValid()) {
							form.getForm().submit({
								url : '../codeValueController/save.action',
								params:{codeId:'<%=request.getParameter("codeId")%>'},
								scope : form,
								success : function(form, action) {
									win.hide();
									grid.getStore().reload();
									grid.getSelectionModel().clearSelections();
								},
								failure : function(form, action) {
									 if (action.result.message == undefined)
									      Ext.Msg.alert('错误', '网络连接失败.');
									    else
									      Ext.Msg.alert('错误', action.result.message);
								},
								waitMsg : '保存数据...'
							});
						} else {
							Ext.Msg.alert('提示', '表单含有无效或空数据');
						}
					},
					id : "btnSave"
				}, {
					text : "关闭",
					id : "btnClose",
					handler : function() {
						win.hide();
					}
				} ],
				layout : 'fit',
				plain : false,
				closable : false,
				modal : true,
				items : [ form ]
			});
		}
		win.show();
	}
	
  
	
	
	// 创建form表单
	function createForm() {
		Ext.define('userForm', {
			extend : 'Ext.form.Panel',
			fields : new Ext.util.MixedCollection(),
			initComponent : function() {
				var me = this;
				var tn = 1;

				this.fields.add("id", new Ext.form.Hidden({
					name : "id",
					dataIndex : "id"
				}));
			
				this.fields.add("itemText", new Ext.form.TextField({
					name : "itemText",
					dataIndex : "itemText",
					fieldLabel : "显示名称",
					allowBlank : false
				}));
				this.fields.add("itemValue", new Ext.form.TextField({
					name : "itemValue",
					dataIndex : "itemValue",
					fieldLabel : "值",
					allowBlank : true
				}));
				this.fields.add("description", new Ext.form.TextField({
					name : "description",
					dataIndex : "description",
					fieldLabel : "描述",
					allowBlank : true
				}));
				this.fields.add("sequence", new Ext.form.NumberField({
					name : "sequence",
					dataIndex : "sequence",
					fieldLabel : "排序",
					allowBlank : true
				}));
				
				this.items = [ {
					layout : "form",
					columnWidth : 1,
					labelAlign : "right",
					labelWidth : 100,
					items : [ this.fields.get("id"), 
					          this.fields.get("itemText"),this.fields.get("itemValue")
					          ,this.fields.get("description"),this.fields.get("sequence")]
				} ];
				this.frame = true;
				this.layout = 'fit';
				this.region = 'east';
				this.border = true;
				this.split = true;
				this.callParent(arguments);
			}
			,
			onRender : function() {
				this.callParent(arguments);
			},
			newDataRecord : function() {
				this.fields.get("id").setValue('');
				this.fields.get("itemValue").setValue('');
				this.fields.get("itemText").setValue('');
				this.fields.get("sequence").setValue('0');
				this.fields.get("description").setValue('');
			}
		});
	}

	// 新建或修改表单成功提交后,重新加载其父节点
	function afterCommitFormSuccess(operation, recordData) {
		onWinHide();
		grid.getStore().reload();
	}
	
	// 创建新的记录，初始化表单
	function EditRecord() {
		var sm = grid.getSelectionModel();
		var records = sm.getSelection();
		
		if (records.length <= 0) {
			Ext.Msg.alert('错误', "未选中记录，不能修改！");
			return;
		}
		var data = records[0].data;
		form.getForm().findField('id').setValue(data.id);
		form.getForm().findField('itemValue').setValue(data.itemValue);
		form.getForm().findField('itemText').setValue(data.itemText);
		form.getForm().findField('description').setValue(data.description);
		form.getForm().findField('sequence').setValue(data.sequence);
		createWindows();

	}
	

	// 创建layout
	function createLayout() {
		toolbar = new Ext.Toolbar({
			items : [ {
				text : "新增",
				iconCls : 'addIcon',
				id : "tlb_NEW",
				handler : function() {
					form.newDataRecord();
					createWindows();
				}
			}, '-', {
				text : "编辑",
				iconCls : 'editIcon',
				id : "tlb_EDIT",
				handler : function() {
					EditRecord();
				}
			}, '-', {
				text : "删除",
				iconCls : 'deleteIcon',
				id : "tlb_DELETE",
				handler : function() {
					grid.deleteRecord();
				}
			}, "->",  searchFlds.get("name"), {
		        text: "搜索",
		        iconCls : 'searchIcon',
		        handler: function () {
		        	grid.load();
		        }
		    } ]
		});

		var viewport = new Ext.Viewport({
			layout : 'border',
			items : [ {
				layout : 'fit',
				region : 'north',
				border : false,
				items : [ toolbar ],
				height : 25
			}, {
				layout : 'border',
				region : 'center',
				border : false,
				items : [ grid ]
			} ]
		});
		
		viewport.doLayout();
		reloadRecords();
	}

</script>
</head>
<body>
</body>
</html>